package com.sunyard.domee.commons.model;


import com.sunyard.domee.commons.exception.ErrorCodeService;

import java.io.Serializable;
import java.text.MessageFormat;

/**
 * @param <T>
 * @author ailev
 */
public class RPCResultModel<T> implements Serializable {
    public static final int RPC_REST_SUCCESS_CODE = 0;

    /**
     * 消息
     */
    private String message = "success";

    /**
     * 编码
     */
    private int code = 0;

    /**
     * 响应实体
     */
    private T data;

    public RPCResultModel() {
    }

    public RPCResultModel(T data) {
        this.data = data;
    }

    public RPCResultModel(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public RPCResultModel(ErrorCodeService error, Object... arguments) {
        this.code = error.getCode();
        this.message = MessageFormat.format(error.getMessage(), arguments);
    }

    public RPCResultModel(ErrorCodeService error) {
        this.code = error.getCode();
        this.message = error.getMessage();
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setError(ErrorCodeService error) {
        this.code = error.getCode();
        this.message = error.getMessage();
    }

    public void setError(ErrorCodeService error, Object... arguments) {
        this.code = error.getCode();
        this.message = MessageFormat.format(error.getMessage(), arguments);
    }

    public boolean isSuccess() {
        return this.code == RPC_REST_SUCCESS_CODE;
    }

}
